/*
 * 描          述:  <描述>
 * 修  改   人:  PengQingyang
 * 修改时间:  2024年3月9日
 * <修改描述:>
 */
package com.tx.core.mybatis.config;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;

/**
 * 自定义处理MybatisPlus的属性值的扩展方法<br/>
 *    在该类中将自定义模块中所需的属性进行添加：
 *    sqlMap的加载
 *    typehandler的注册
 * <功能详细描述>
 * 
 * @author  PengQingyang
 * @version  [版本号, 2024年3月9日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class TxMybatisPlusPropertiesCustomizer
        implements MybatisPlusPropertiesCustomizer {
    
    private String[] componentSqlMapLocations = new String[] {
            "classpath*:com/tx/component/**/*SqlMap_BASICDATA.xml",
            "classpath*:com/tx/component/**/*SqlMap_CONFIGURATION.xml",
            "classpath*:com/tx/component/**/*SqlMap_PLUGIN.xml",
            "classpath*:com/tx/component/**/*SqlMap_SECURITY.xml",
            "classpath*:com/tx/component/**/*SqlMap_FILE.xml",
            "classpath*:com/tx/component/**/*SqlMap_TASK.xml",
            "classpath*:com/tx/component/**/*SqlMap_CMU.xml" };
    
    @Override
    public void customize(MybatisPlusProperties properties) {
        //将扩展的TypeHandler写入
        applyTypeHandlerCustomizer(properties);
        //将component模块中对应的sqlmap集写入
        applyComponentSqlMapLocation(properties);
    }
    
    /**
     * 将自定义的typeHandler写入
     * <功能详细描述>
     * @param properties [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    private void applyTypeHandlerCustomizer(MybatisPlusProperties properties) {
        String typeHandlerPackage = properties.getTypeHandlersPackage();
        Set<String> typeHandlerPackageSet = new HashSet<>();
        if (!StringUtils.isEmpty(typeHandlerPackage)) {
            Arrays.asList(StringUtils.splitByWholeSeparator(typeHandlerPackage,
                    typeHandlerPackage.trim())).forEach(p -> {
                        if (!StringUtils.isBlank(p.trim())) {
                            typeHandlerPackageSet.add(p.trim());
                        }
                    });
        }
        typeHandlerPackageSet.add("com.tx.core.mybatis.handler");
    }
    
    /**
     * 将Component模块的SqlMapLocation写入<br/>
     * <功能详细描述>
     * @param properties [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    private void applyComponentSqlMapLocation(
            MybatisPlusProperties properties) {
        String[] locations = properties.getMapperLocations();
        Set<String> locationSet = new HashSet<>(Arrays.asList(locations));
        Arrays.asList(componentSqlMapLocations).forEach(l -> {
            locationSet.add(l);
        });
        properties.setMapperLocations(
                locationSet.toArray(new String[locationSet.size()]));
    }
}
